<?php
/**
 * This file runs all unit tests for PHP server faces. By default 
 * it does not modify the path, therefore it tests the installed PEAR
 * version of the application. This is default, but not the most logical
 * for development; who wants to install a pear copy every time you want to
 * run test? who CAN if you only have one machine? Therefore, if you run
 * it with the command line argument "--devel" it will add the source directory
 * to the beginning of the include directory. A current limitation, mainly
 * due to my laziness, is that you must execute this file from within the
 * tests directory. Hey, it was this header or fixing that.
 * 
 * @license http://www.gnu.org/licenses/lgpl.html LGPL
 * @author Harlan Iverson <h dot iverson at gmail dot com>
 * @copyright Copyright (c) 2006, Harlan Iverson.
 * @version $Id: AllTests.php 20 2006-08-01 05:54:40Z Harlan $
 */

if( array_search( "--help", $argv ) !== false ) {
	die( <<<HELP

--help				displaysthis message
--devel				runs tests from within the checkout, 
				as opposed to the current PEAR installation
--display-errors		displays errors
--skip-coverage			explicityly skips the generation of code coverage reports

HELP
	);
}


ini_set( "display_errors", array_search( "--display-errors", $argv ) !== false ? "On" : "Off" );
 
define( "PHPUnit_MAIN_METHOD", "AllTests::main" );
define( "XDEBUG_LOADED", extension_loaded("xdebug") ? true : false );
define( "ENABLE_COVERAGE_REPORT", XDEBUG_LOADED && array_search( "--skip-coverage", $argv )!==false);




$facesPath = dirname( getcwd() );
$testPath = "$facesPath/tests";

if( array_search( "--devel", $argv ) !== false ) {
	set_include_path( "$facesPath:".get_include_path() );
}


require_once 'PHPUnit.php';
require_once 'ResolverTest.php';
require_once 'test-facelets2smarty.php';
require_once 'test-lifecycle.php';
require_once 'test-components.php';
//require_once 'test-faces-config.php';

class AllTests {

	public static function main() {
		$result = PHPUnit::run( self::suite() );
		echo $result->toString();	
	}

    public static function suite() {

		$suite  = new PHPUnit_TestSuite();
		$suite->addTestSuite( "PhpFaces_Test_ResolverTest" );
		//$suite->addTestSuite( "FacesConfigTest" );
		//$suite->addTestSuite( "Facelets2SmartyTest" );
		//$suite->addTestSuite( "FaceletsTest" );
		$suite->addTestSuite( "PhpFaces_Test_Lifecycle_PhaseListenerTest" );
		$suite->addTestSuite( "ComponentsTest" );

        return $suite;
    }
}


$logger = Logger::getLogger( "PhpFaces.Test" );
$logger->info( "Started tests [$testPath] for codebase [$facesPath]" );
if( ENABLE_COVERAGE_REPORT ) {
	$logger->info( "Using Spike PHPCoverage" );
} else {
	$logger->warn( "Running without Spike PHPCoverage.");
}


if( ENABLE_COVERAGE_REPORT ) {
	define( "PHPCOVERAGE_HOME", "/usr/local/spikephpcoverage/src/" );
	
	require_once( PHPCOVERAGE_HOME . "CoverageRecorder.php" );
	require_once( PHPCOVERAGE_HOME . "reporter/HtmlCoverageReporter.php" );
	
	$reporter = new HtmlCoverageReporter("Code Coverage Report", "", "report");
	
	$includePaths = array( $facesPath );
	$excludePaths = array( $testPath );
	$cov = new CoverageRecorder($includePaths, $excludePaths, $reporter);
	
	$cov->startInstrumentation();
}



if( PHPUnit_MAIN_METHOD == 'AllTests::main' ) {
	AllTests::main();
}

if( ENABLE_COVERAGE_REPORT ) {
	$cov->stopInstrumentation();
	
	$cov->generateReport();
	$reporter->printTextSummary();
}

?>